#include "tools/memChecker.h"

#include <windows.h>

using namespace sd;
using namespace tools;
using namespace system;





MemChecker::MemChecker()
{
	HMODULE lib = LoadLibrary(TEXT("Kernel32.lib"));
	if(lib)
	{
		ptr_ = GetProcAddress(lib, "IsBadReadPtr");
	}
}


bool MemChecker::read(const void* ptr, size_t size)const
{
	typedef BOOL (WINAPI *TIsBadReadPtr)( const VOID *lp, UINT_PTR ucb);

	if( ptr_ && (static_cast<TIsBadReadPtr>(ptr_))(ptr, size)!=0)
		return false;

	__try
	{
		for( const char* it = static_cast<const char*>(ptr); it != static_cast<const char*>(ptr)+size; ++it)
		{
			volatile char ch = *it;
		}

	}__except(EXCEPTION_EXECUTE_HANDLER)
	{
		return false;
	}
	return true;
}
